global root_dir = "`1'"

include "$root_dir/code/config/config.do"

cap noi log using ${log_dir}/Table_A19_keywords_correlation.log, replace name(tabl)

capture noi {

    * Cross-correlation table of main keywords

    texdoc init ${tab_dir}/appendix/Table_A19_keywords_correlation.tex, replace
    texdoc stlog, nolog


    * get statistics
    import delim ${classification_dir}/V6/ipc6XX_tf.csv, varnames(1) clear

    *restrict to machinery sector
    gen machinery_field = techn_sector == "Mechanical engineering" & (techn_field == "Handling" ///
        | techn_field == "Machine tools" | techn_field == "Other special machines" ///
        | techn_field == "Textile and paper machines")
    rename ipc6 cipc6
    gen cipc3 = substr(cipc6, 1, 3)
    gen cipc4 = substr(cipc6, 1, 4)
    replace machinery_field = 0 if cipc3 == "F41" | cipc3 == "F42"
    replace machinery_field = 1 if cipc4 == "B42C"
    replace machinery_field = 1 if cipc4 == "B07C"
    replace machinery_field = 1 if cipc6 == "G05B19"
    replace machinery_field = 1 if cipc6 == "B62D65"
    gen cipc1 = substr(cipc6, 1, 1)
    assert machinery_field == 0 if cipc1 == "Y"
    replace techn_field = "non-machinery" if machinery_field == 0
    replace techn_field = "non-classified" if cipc1 == "Y"
    rename (cipc6 machinery_field) (ipc6 machinery)

    keep share_* machinery
    rename share_* *
    *get correlation and save to table
    estpost correlate cnc automat labor robot if machinery == 1, matrix
    matrix list e(b)
    sleep 500
    texdoc local cnc_cnc : display %9.3f e(b)[1,1]
    sleep 500
    texdoc local cnc_automat : display %9.3f e(b)[1,2]
    sleep 500
    texdoc local cnc_labor : display %9.3f e(b)[1,3]
    sleep 500
    texdoc local cnc_robot : display %9.3f e(b)[1,4]
    sleep 500
    texdoc local automat_automat : display %9.3f e(b)[1,5]
    sleep 500
    texdoc local automat_labor : display %9.3f e(b)[1,6]
    sleep 500
    texdoc local automat_robot : display %9.3f e(b)[1,7]
    sleep 500
    texdoc local labor_labor : display %9.3f e(b)[1,8]
    sleep 500
    texdoc local labor_robot : display %9.3f e(b)[1,9]
    sleep 500
    texdoc local robot_robot : display %9.3f e(b)[1,10]
    sleep 500

    * output table (via texdoc)
    texdoc local notes "This table shows the correlation between the prevalence of the main keywords, computed for C/IPC 6-digit codes."
    texdoc stlog close
    qui include ${code_dir}/config/texdoctools.do
    tex `texpreamble'
    /*tex
    \begin{tabular}{@{}l*{4}{c}@{}}
    \toprule
    Keywords  & Automat & Robot & CNC & Labor \\
    \midrule
    Automat 	  & `automat_automat' &     &    &       \\
    Robot 	  & `automat_robot' & `robot_robot' &  &    \\
    CNC 		  & `cnc_automat' & `cnc_robot' & `cnc_cnc' &       \\
    Labor 	  & `automat_labor' & `labor_robot' & `cnc_labor' & `labor_labor' \\
    \bottomrule
    \end{tabular}
    tex*/
    tex `texpostamble'
    texdoc close

}
if _rc == 0 {
    display "Execution finished successfully."
}
else {
    display "Execution finished with errors."
}